package cn.yuemouren.leetcode;

/**
 * @Author: Timi
 * @Description: 516. 最长回文子序列 使用动态规划
 * @Date: 2020/8/18 14:42
 */
public class Test_516 {

    public int longestPalindromeSubseq(String s) {

        int length = s.length();
        int[][] h = new int[length][length];
        for ( int i = length - 1; i >= 0; i--){
            h[i][i] = 1;
            for (int j = i + 1; j < length; j++){
                if(s.charAt(i) == s.charAt(j)){
                    h[i][j] = h[i + 1][j - 1] + 2;
                } else {
                    h[i][j] = Math.max(h[i + 1][j],h[i][j - 1]);
                }
            }
        }
        return h[0][length-1];
    }
}
